Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 478)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.87366 12.87819 12.88270 12.88715 12.89156 12.89591 12.90019 12.90439
## [9] 12.90852 12.91254 12.91647 12.92029 12.92399 12.92756 12.93100 12.93430
## [17] 12.93745 12.94043 12.94326 12.94590 12.94836 12.95063 12.95272 12.95465
## [25] 12.95643 12.95807 12.95957 12.96093 12.96217 12.96329 12.96429 12.96517
## [33] 12.96596 12.96664 12.96723 12.96773 12.96815 12.96849 12.96876 12.96896
## [41] 12.96911 12.96920 12.96924 12.96924 12.96920 12.96912 12.96903 12.96891
## [49] 12.96877 12.96863 12.96840 12.96800 12.96744 12.96673 12.96587 12.96489
## [57] 12.96378 12.96255 12.96121 12.95977 12.95825 12.95664 12.95495 12.95321
## [65] 12.95140 12.94955 12.94766 12.94574 12.94380 12.94184 12.93988 12.93793
## [73] 12.93598 12.93406 12.93217 12.93031 12.92850 12.92675 12.92444 12.92102
## [81] 12.91660 12.91129 12.90520 12.89845 12.89114 12.88339 12.87530 12.86699
## [89] 12.85856 12.85014 12.84182 12.83372 12.82595 12.81863 12.81185 12.80574
## [97] 12.80040 12.79594 12.79248 12.78907 12.78476 12.77964 12.77379 12.76729
## [105] 12.76022 12.75267 12.74473 12.73647 12.72799 12.71936 12.71066 12.70199
## [113] 12.69343 12.68506 12.67695 12.66921 12.66191 12.65513 12.64897 12.64349
## [121] 12.63880 12.63496 12.63207 12.63020 12.62945 12.62989 12.63162 12.63470
## [129] 12.63979 12.64719 12.65654 12.66745 12.67953 12.69240 12.70569 12.71901
## [137] 12.73197 12.74420 12.75531 12.76492 12.77409 12.78405 12.79470 12.80593
## [145] 12.81762 12.82967 12.84197 12.85440 12.86687 12.87925 12.89144 12.90333
## [153] 12.91481 12.92577 12.93753 12.95115 12.96610 12.98186 12.99790 13.01371
## [161] 13.02875 13.04252 13.05448 13.06771 13.08525 13.10658 13.13116 13.15846
## [169] 13.18793 13.21904 13.25125 13.28403 13.31684 13.34915 13.38041 13.41009
## [177] 13.43766 13.46258 13.48431 13.50232 13.51607 13.52502 13.53103 13.53632
## [185] 13.54087 13.54467 13.54771 13.54997 13.55144 13.55210 13.55195 13.55096
## [193] 13.54913 13.54644 13.54288 13.53844 13.53309 13.52684 13.51966 13.51155
## [201] 13.50248 13.49245 13.48144 13.46944 13.45551 13.43895 13.42008 13.39924
## [209] 13.37676 13.35298 13.32821 13.30280 13.27707 13.25135 13.22599 13.20130
## [217] 13.17763 13.15148 13.12004 13.08466 13.04668 13.00743 12.96827 12.93052
## [225] 12.89554 12.86465 12.83371 12.79817 12.75883 12.71646 12.67185 12.62579
## [233] 12.57905 12.53243 12.48671 12.44266 12.40109 12.36276 12.32846 12.29899
## [241] 12.27138 12.24246 12.21264 12.18235 12.15201 12.12204 12.09287 12.06491
## [249] 12.03859 12.01432 11.99254 11.97367 11.95732 11.94274 11.92977 11.91825
## [257] 11.90803 11.89894 11.89084 11.88356 11.87694 11.87083 11.86507 11.85951
## [265] 11.85398 11.84832 11.84458 11.84431 11.84661 11.85062 11.85543 11.86017
## [273] 11.86395 11.86589 11.86510 11.86514 11.86966 11.87790 11.88911 11.90256
## [281] 11.91749 11.93315 11.94879 11.96368 11.97705 11.98817 11.99628 12.00064
## [289] 12.00050 11.99878 11.99869 11.99988 12.00200 12.00474 12.00773 12.01065
## [297] 12.01315 12.01489 12.01553 12.01474 12.01216 12.00748 12.00033 11.98966
## [305] 11.97504 11.95704 11.93627 11.91332 11.88876 11.86321 11.83724 11.81145
## [313] 11.78642 11.76276 11.74105 11.72187 11.70583 11.68899 11.66762 11.64264
## [321] 11.61499 11.58557 11.55531 11.52513 11.49596 11.46870 11.44429 11.42364
## [329] 11.40768 11.39276 11.37503 11.35510 11.33357 11.31104 11.28812 11.26542
## [337] 11.24353 11.22308 11.20466 11.18887 11.17632 11.16762 11.16338 11.16212
## [345] 11.16199 11.16298 11.16509 11.16832 11.17267 11.17813 11.18470 11.19238
## [353] 11.20219 11.21494 11.23029 11.24789 11.26742 11.28852 11.31088 11.33414
## [361] 11.35797 11.38204 11.40600 11.42952 11.45225 11.47387 11.49649 11.52216
## [369] 11.55047 11.58098 11.61327 11.64692 11.68151 11.71661 11.75179 11.78663
## [377] 11.82071 11.85361 11.88489 11.91413 11.94465 11.97956 12.01812 12.05959
## [385] 12.10325 12.14837 12.19421 12.24004 12.28512 12.32873 12.37013 12.40859
## [393] 12.44338 12.47376 12.50383 12.53757 12.57412 12.61259 12.65213 12.69184
## [401] 12.73086 12.76831 12.80332 12.83502 12.86253 12.88498 12.90447 12.92361
## [409] 12.94226 12.96030 12.97760 12.99405 13.00952 13.02388 13.03700 13.04876
## [417] 13.05904 13.06772 13.07465 13.07973 13.08129 13.07842 13.07215 13.06348
## [425] 13.05343 13.04301 13.03323 13.02510 13.01964 13.01498 13.00867 13.00086
## [433] 12.99170 12.98135 12.96996 12.95768 12.94467 12.93106 12.91702 12.90270
## [441] 12.88825 12.87382 12.85956 12.84495 12.82940 12.81296 12.79568 12.77760
## [449] 12.75878 12.73927 12.71912 12.69838 12.67709 12.65531 12.63309 12.61032
## [457] 12.58684 12.56268 12.53783 12.51231 12.48613 12.45929 12.43180 12.40367
## [465] 12.37491 12.34553 12.31554 12.28494 12.25374 12.22215 12.19031 12.15816
## [473] 12.12562 12.09262 12.05909 12.02494 11.99011 11.95454
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 478)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.52232 12.52291 12.52350 12.52410 12.52470 12.52531 12.52593 12.52656
## [9] 12.52721 12.52787 12.52856 12.52927 12.53000 12.53077 12.53156 12.53238
## [17] 12.53324 12.53414 12.53508 12.53606 12.53709 12.53816 12.53926 12.54035
## [25] 12.54143 12.54251 12.54359 12.54466 12.54573 12.54680 12.54786 12.54891
## [33] 12.54997 12.55102 12.55207 12.55311 12.55416 12.55520 12.55624 12.55727
## [41] 12.55831 12.55934 12.56038 12.56141 12.56244 12.56347 12.56450 12.56553
## [49] 12.56656 12.56759 12.56861 12.56961 12.57059 12.57156 12.57253 12.57348
## [57] 12.57444 12.57539 12.57634 12.57731 12.57828 12.57926 12.58026 12.58127
## [65] 12.58231 12.58337 12.58446 12.58558 12.58673 12.58792 12.58914 12.59042
## [73] 12.59173 12.59310 12.59451 12.59598 12.59751 12.59910 12.60066 12.60209
## [81] 12.60342 12.60465 12.60580 12.60689 12.60792 12.60891 12.60988 12.61084
## [89] 12.61181 12.61279 12.61380 12.61486 12.61598 12.61717 12.61845 12.61983
## [97] 12.62132 12.62295 12.62472 12.62561 12.62469 12.62212 12.61805 12.61264
## [105] 12.60604 12.59841 12.58990 12.58067 12.57087 12.56066 12.55019 12.53962
## [113] 12.52911 12.51880 12.50886 12.49943 12.49068 12.48276 12.47582 12.47002
## [121] 12.46551 12.46245 12.46100 12.46130 12.46352 12.46781 12.47505 12.48549
## [129] 12.49836 12.51288 12.52826 12.54372 12.55848 12.57177 12.58279 12.59432
## [137] 12.60931 12.62725 12.64763 12.66993 12.69365 12.71828 12.74330 12.76821
## [145] 12.79249 12.81564 12.83714 12.85649 12.87316 12.89030 12.91091 12.93429
## [153] 12.95976 12.98661 13.01415 13.04169 13.06853 13.09397 13.11733 13.13791
## [161] 13.15501 13.17176 13.19139 13.21334 13.23706 13.26199 13.28758 13.31327
## [169] 13.33851 13.36274 13.38541 13.40595 13.42383 13.43848 13.44934 13.45801
## [177] 13.46638 13.47441 13.48204 13.48919 13.49583 13.50187 13.50728 13.51197
## [185] 13.51590 13.51901 13.52124 13.52252 13.52280 13.52201 13.52011 13.51702
## [193] 13.51269 13.50705 13.50006 13.49165 13.48175 13.47032 13.45675 13.44070
## [201] 13.42245 13.40230 13.38053 13.35744 13.33331 13.30844 13.28311 13.25762
## [209] 13.23226 13.20731 13.18308 13.15983 13.13452 13.10445 13.07049 13.03349
## [217] 12.99432 12.95385 12.91292 12.87242 12.83319 12.79611 12.76202 12.73181
## [225] 12.70122 12.66601 12.62700 12.58503 12.54090 12.49547 12.44954 12.40396
## [233] 12.35955 12.31714 12.27755 12.24161 12.21016 12.18402 12.15901 12.13113
## [241] 12.10146 12.07107 12.04104 12.01246 11.98639 11.96392 11.94613 11.93184
## [249] 11.91907 11.90775 11.89777 11.88906 11.88153 11.87508 11.86963 11.86509
## [257] 11.86138 11.85839 11.85605 11.85427 11.85295 11.85518 11.86337 11.87641
## [265] 11.89321 11.91267 11.93368 11.95515 11.97598 11.99507 12.01131 12.02362
## [273] 12.03088 12.03771 12.04893 12.06378 12.08150 12.10134 12.12253 12.14432
## [281] 12.16593 12.18662 12.20562 12.22217 12.23552 12.24489 12.24954 12.25227
## [289] 12.25619 12.26096 12.26626 12.27174 12.27709 12.28197 12.28605 12.28900
## [297] 12.29049 12.29019 12.28777 12.28289 12.27523 12.26301 12.24522 12.22271
## [305] 12.19632 12.16690 12.13527 12.10229 12.06880 12.03564 12.00364 11.97366
## [313] 11.94653 11.92310 11.90421 11.88426 11.85827 11.82796 11.79507 11.76133
## [321] 11.72848 11.69825 11.67237 11.65257 11.63528 11.61603 11.59524 11.57333
## [329] 11.55069 11.52775 11.50492 11.48262 11.46125 11.44122 11.42296 11.40687
## [337] 11.39337 11.38287 11.37332 11.36264 11.35127 11.33960 11.32806 11.31705
## [345] 11.30698 11.29828 11.29136 11.28661 11.28447 11.28534 11.28863 11.29343
## [353] 11.29965 11.30723 11.31608 11.32613 11.33730 11.34952 11.36270 11.37677
## [361] 11.39166 11.40728 11.42356 11.44042 11.46013 11.48456 11.51303 11.54485
## [369] 11.57935 11.61584 11.65364 11.69207 11.73045 11.76810 11.80433 11.83846
## [377] 11.86982 11.89772 11.92557 11.95684 11.99095 12.02732 12.06537 12.10451
## [385] 12.14417 12.18376 12.22271 12.26043 12.29634 12.32985 12.36040 12.38740
## [393] 12.41381 12.44268 12.47352 12.50585 12.53919 12.57306 12.60698 12.64046
## [401] 12.67302 12.70419 12.73347 12.76040 12.78448 12.80524 12.82411 12.84279
## [409] 12.86116 12.87911 12.89654 12.91333 12.92937 12.94456 12.95879 12.97194
## [417] 12.98390 12.99457 13.00384 13.01159 13.01740 13.02114 13.02318 13.02386
## [425] 13.02355 13.02260 13.02136 13.02020 13.01948 13.01846 13.01626 13.01293
## [433] 13.00855 13.00319 12.99693 12.98982 12.98195 12.97339 12.96420 12.95445
## [441] 12.94423 12.93359 12.92261 12.91102 12.89850 12.88510 12.87085 12.85580
## [449] 12.83998 12.82343 12.80619 12.78830 12.76980 12.75071 12.73109 12.71086
## [457] 12.68991 12.66824 12.64586 12.62277 12.59897 12.57446 12.54924 12.52332
## [465] 12.49668 12.46935 12.44131 12.41257 12.38312 12.35306 12.32244 12.29124
## [473] 12.25941 12.22694 12.19380 12.15996 12.12538 12.09005
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 478)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.69673 11.70869 11.72057 11.73234 11.74400 11.75553 11.76691 11.77813
## [9] 11.78917 11.80003 11.81068 11.82112 11.83132 11.84128 11.85097 11.86039
## [17] 11.86952 11.87834 11.88685 11.89502 11.90284 11.91030 11.91738 11.92407
## [25] 11.93035 11.93621 11.94164 11.94662 11.95113 11.95518 11.95881 11.96202
## [33] 11.96485 11.96730 11.96940 11.97116 11.97260 11.97375 11.97462 11.97523
## [41] 11.97560 11.97574 11.97568 11.97543 11.97502 11.97446 11.97376 11.97296
## [49] 11.97207 11.97110 11.97007 11.96902 11.96794 11.96686 11.96581 11.96479
## [57] 11.96383 11.96231 11.95967 11.95598 11.95132 11.94577 11.93940 11.93229
## [65] 11.92453 11.91618 11.90732 11.89804 11.88841 11.87851 11.86841 11.85820
## [73] 11.84794 11.83773 11.82763 11.81773 11.80810 11.79882 11.78997 11.78162
## [81] 11.77386 11.76675 11.76038 11.75483 11.75018 11.74466 11.73667 11.72648
## [89] 11.71438 11.70064 11.68556 11.66940 11.65245 11.63500 11.61731 11.59968
## [97] 11.58238 11.56570 11.54992 11.53531 11.52217 11.51076 11.50138 11.49429
## [105] 11.48979 11.48816 11.48819 11.48852 11.48919 11.49023 11.49168 11.49356
## [113] 11.49590 11.49875 11.50213 11.50608 11.51063 11.51581 11.52166 11.52821
## [121] 11.53549 11.54353 11.55238 11.56205 11.57259 11.58402 11.59638 11.60971
## [129] 11.62646 11.64848 11.67484 11.70460 11.73685 11.77066 11.80510 11.83925
## [137] 11.87218 11.90297 11.93069 11.95441 11.97807 12.00577 12.03687 12.07070
## [145] 12.10662 12.14395 12.18206 12.22027 12.25794 12.29441 12.32902 12.36112
## [153] 12.39005 12.41515 12.43990 12.46760 12.49732 12.52813 12.55911 12.58935
## [161] 12.61791 12.64387 12.66630 12.68714 12.70882 12.73117 12.75400 12.77716
## [169] 12.80047 12.82376 12.84686 12.86959 12.89179 12.91328 12.93389 12.95344
## [177] 12.97178 12.98872 13.00410 13.01774 13.02946 13.03911 13.04740 13.05513
## [185] 13.06226 13.06876 13.07457 13.07967 13.08400 13.08753 13.09021 13.09201
## [193] 13.09288 13.09278 13.09168 13.08952 13.08627 13.08189 13.07633 13.06956
## [201] 13.06153 13.05220 13.04153 13.02949 13.01591 13.00074 12.98410 12.96609
## [209] 12.94682 12.92640 12.90493 12.88251 12.85927 12.83531 12.81073 12.78564
## [217] 12.76016 12.73056 12.69427 12.65304 12.60867 12.56292 12.51758 12.47441
## [225] 12.43519 12.40169 12.36790 12.32738 12.28131 12.23087 12.17725 12.12161
## [233] 12.06515 12.00903 11.95445 11.90257 11.85458 11.81166 11.77499 11.74574
## [241] 11.71808 11.68609 11.65080 11.61325 11.57449 11.53555 11.49748 11.46131
## [249] 11.42809 11.39886 11.37465 11.35651 11.34416 11.33622 11.33213 11.33129
## [257] 11.33315 11.33711 11.34261 11.34906 11.35589 11.36252 11.36837 11.37287
## [265] 11.37544 11.37549 11.38009 11.39480 11.41686 11.44348 11.47190 11.49934
## [273] 11.52302 11.54016 11.54800 11.55364 11.56531 11.58197 11.60255 11.62600
## [281] 11.65126 11.67727 11.70298 11.72733 11.74925 11.76771 11.78163 11.78996
## [289] 11.79164 11.78814 11.78182 11.77300 11.76197 11.74904 11.73453 11.71872
## [297] 11.70193 11.68445 11.66661 11.64869 11.63102 11.61388 11.59758 11.57724
## [305] 11.54869 11.51329 11.47244 11.42750 11.37985 11.33088 11.28195 11.23445
## [313] 11.18976 11.14924 11.11429 11.08627 11.06657 11.05074 11.03366 11.01574
## [321] 10.99736 10.97891 10.96079 10.94338 10.92709 10.91229 10.89939 10.88878
## [329] 10.88084 10.87513 10.87089 10.86800 10.86638 10.86591 10.86650 10.86804
## [337] 10.87044 10.87358 10.87738 10.88173 10.88652 10.89166 10.89705 10.90401
## [345] 10.91359 10.92528 10.93856 10.95290 10.96779 10.98272 10.99715 11.01058
## [353] 11.02477 11.04160 11.06072 11.08180 11.10447 11.12841 11.15326 11.17868
## [361] 11.20433 11.22986 11.25493 11.27918 11.30229 11.32390 11.34574 11.36960
## [369] 11.39515 11.42210 11.45014 11.47896 11.50827 11.53776 11.56713 11.59607
## [377] 11.62427 11.65144 11.67726 11.70145 11.72520 11.74985 11.77521 11.80112
## [385] 11.82741 11.85392 11.88048 11.90691 11.93306 11.95875 11.98382 12.00809
## [393] 12.03141 12.05360 12.07730 12.10475 12.13512 12.16761 12.20140 12.23567
## [401] 12.26963 12.30246 12.33333 12.36145 12.38600 12.40618 12.42397 12.44185
## [409] 12.45967 12.47727 12.49451 12.51125 12.52733 12.54262 12.55696 12.57021
## [417] 12.58222 12.59285 12.60194 12.60935 12.61493 12.61879 12.62118 12.62238
## [425] 12.62264 12.62223 12.62142 12.62045 12.61961 12.61837 12.61607 12.61278
## [433] 12.60853 12.60338 12.59739 12.59059 12.58304 12.57480 12.56591 12.55643
## [441] 12.54640 12.53588 12.52492 12.51334 12.50095 12.48776 12.47379 12.45906
## [449] 12.44359 12.42738 12.41046 12.39284 12.37454 12.35557 12.33595 12.31566
## [457] 12.29463 12.27289 12.25042 12.22724 12.20333 12.17870 12.15336 12.12730
## [465] 12.10053 12.07305 12.04485 12.01594 11.98632 11.95606 11.92521 11.89373
## [473] 11.86162 11.82884 11.79538 11.76122 11.72633 11.69068
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")